using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;

public class Glove : MonoBehaviour
{
    public int playerIndex;
    public KeyCode fireKey;
    public List<string> fireAudios;
    public float fireForce;
    public float holdTime;
    public float rotateTorque;
    public Transform root;
    public Vector3 playerOffset;
    public List<Transform> joints;
    public SpriteShapeController shape;

    public bool InWater;
    private Rigidbody2D m_rigidbody;
    private float m_holdTime;
    private float m_coldTime;

    private void Awake()
    {
        InWater = true;
        m_rigidbody = GetComponent<Rigidbody2D>();
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //Debug.Log("1:" + shape.spline.GetPosition(0));
        //Debug.Log("2:" + shape.spline.GetPosition(1));
        //Debug.Log("3:" + shape.spline.GetPosition(2));
        //Debug.Log("4:" + shape.spline.GetPosition(3));
        //shape.spline.Clear();
        shape.spline.SetPosition(0, transform.position - playerOffset);
        int jointsCount = 0;
        foreach (var curJoint in joints)
        {
            jointsCount++;
            var jointPos = curJoint.position - playerOffset;
            shape.spline.SetPosition(jointsCount, jointPos);
        }
        jointsCount++;
        shape.spline.SetPosition(jointsCount, root.parent.position + root.localPosition / 2.0f - playerOffset);

        jointsCount = 0;
        var firePos = transform.rotation * Vector2.right * 1.0f;
        shape.spline.SetRightTangent(jointsCount, -firePos);
        foreach (var curJoint in joints)
        {
            jointsCount++;
            var leftPos = shape.spline.GetPosition(jointsCount - 1);
            var rightPos = shape.spline.GetPosition(jointsCount + 1);
            var offset = (leftPos - rightPos) / 6.0f;
            shape.spline.SetLeftTangent(jointsCount, offset);
            shape.spline.SetRightTangent(jointsCount, -offset);
        }
        jointsCount++;
        shape.spline.SetLeftTangent(jointsCount, root.localPosition * root.parent.localScale.x * 0.6f);

        if (GameManager.Instance.isPause)
            return;

        m_coldTime -= Time.deltaTime;
        if (m_coldTime > 0.0f)
            return;

        if (Input.GetKeyDown(fireKey))
        {
            m_holdTime = 0;
        }
        if (Input.GetKeyUp(fireKey))
        {
            var randomIndex = Random.Range(0, fireAudios.Count);
            AudioManager.Instance.PlaySE(fireAudios[randomIndex]);

            m_rigidbody.AddForce(firePos.normalized * fireForce);
            m_coldTime = 1.0f;
        }
    }

    private void FixedUpdate()
    {
        if (GameManager.Instance.isPause)
            return;

        if (m_coldTime > 0.0f)
            return;

        if (Input.GetKey(fireKey))
        {
            m_holdTime += Time.fixedDeltaTime;
            m_rigidbody.AddTorque((m_holdTime - holdTime) * rotateTorque);
        }
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        var army = collision.collider.GetComponent<SoilNutrient>();
        if (army)
            army.Hitted(this);
    }

    public Vector2 GetVelocity()
    {
        return m_rigidbody.velocity;
    }
}
